package cn.com.lsjz.nursemanager.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.com.lsjz.commons.utils.UpdateTool;
import cn.com.lsjz.nursemanager.model.system.SysRole;
import cn.com.lsjz.nursemanager.model.system.SysRoleVsOperation;
import cn.com.lsjz.nursemanager.repository.system.SysRoleRepository;
import cn.com.lsjz.nursemanager.repository.system.SysRoleVsOperationReposity;


@Service
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleRepository sysroleRepository;
	
	@Autowired
	private SysRoleVsOperationReposity sysRoleVsOperationReposity;
	
	@Override
	public String getRolesByUserId(String userId) {
		List<SysRole> roles = sysroleRepository.loadByUserId(userId);
		if(roles.size() > 0) {
			StringBuilder roleStr = new StringBuilder();
			int ii = 0;
			for (SysRole role : roles) {
				roleStr.append(ii++ == 0 ? role.getRoleName() : "," + role.getRoleName());
			}
			return roleStr.toString();
		}else {
			return "护士";
		}
	}

	@Override
	public SysRole saveRole(SysRole role) {
		role.setActive(1);
		return sysroleRepository.save(role);
	}

	@Override
	public SysRole editRole(SysRole role) {
		SysRole role2 = sysroleRepository.findOne(role.getId());
		UpdateTool.copyNullProperties(role2, role);
		return sysroleRepository.save(role);
	}

	@Override
	public void delRole(Integer id) {
		sysroleRepository.delete(id);
	}

	@Override
	public SysRole getRole(Integer id) {
		return sysroleRepository.findOne(id);
	}

	@Override
	public void activeRole(Integer id, Integer active) {
		sysroleRepository.updateByHql(" update Role r set r.active = ?1 where r.id = ?2 ", active, id);
	}

	@Override
	public List<SysRole> loadRoleList(String roleName) {
		List<SysRole> roles = null;
		if(StringUtils.isEmpty(roleName.trim()))
			roles = sysroleRepository.findAll();
		else
			roles = sysroleRepository.getByRoleNameContaining(roleName);
		return roles;
	}

	@Override
	public Map<String, Object> getPermission(Integer roleId) {
		Map<String, Object> map = new HashMap<>();
		List<SysRoleVsOperation> roleVsModules = sysRoleVsOperationReposity.getByRoleId(roleId);
		map.put("roleVsModules", roleVsModules);
		return map;
	}

	@Override
	public void savePermission(Integer roleId, String modules) {
		List<SysRoleVsOperation> roleVsModules = sysRoleVsOperationReposity.getByRoleId(roleId);
		List<Integer> moduleStrList = new ArrayList<>();
		// 删除
		for (SysRoleVsOperation roleVsModule : roleVsModules) { // 科室
			if (!modules.contains(roleVsModule.getOperationId() + ",")) { // 传入的里面不包含当前，表示这个不需要了
				sysRoleVsOperationReposity.delete(roleVsModule);
			}
			moduleStrList.add(roleVsModule.getOperationId());
		}
		// 新增
		for (String operationId : modules.split(",")) {
			if(StringUtils.isEmpty(operationId))
				continue;
			if (!moduleStrList.contains(Integer.parseInt(operationId))) {
				SysRoleVsOperation roleVsModule = new SysRoleVsOperation();
				roleVsModule.setOperationId(Integer.parseInt(operationId));
				roleVsModule.setRoleId(roleId);
				sysRoleVsOperationReposity.save(roleVsModule);
			}
		}
	}

}
